package com.zhiwang.planet.ui.main


import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.flyco.tablayout.listener.OnTabSelectListener
import com.zhiwang.planet.R
import com.zhiwang.planet.event.MobileEvent
import com.zhiwang.planet.sdk.AdListActivity
import com.zhiwang.planet.ui.*
import com.zhiwang.planet.util.Store
import com.zhiwang.planet.util.TokenManager
import com.zhiwang.planet.util.quickStartActivity
import com.zhiwang.planet.widget.StyleTab
import kotlinx.android.synthetic.main.fragment_money.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * A simple [BaseFragment] subclass.
 *
 */
class MoneyFragment : BaseFragment() {

    private val TAG = "MoneyFragment"

    lateinit var statusBarHandler: FragmentChangeListener
    lateinit var fragments: ArrayList<Fragment>
    private var currentTab = 0

    var fragmentChangeManager: FragmentChangeManager? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_money, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        fragments = arrayListOf(
                WebViewFragment.newInstance(Store.URL_TAKS, false),
                WebViewFragment.newInstance(Store.URL_MOBILE, false),
                WebViewFragment.newInstance(Store.URL_POKER, false),
                EmptyFragment()
        )


        fragmentChangeManager = FragmentChangeManager(childFragmentManager, R.id.container, fragments)


        myTaskbutton.setOnClickListener {
            WebViewActivity.load(requireContext(), Store.URL_MY_GAME)
        }

        search_layout.setOnClickListener {

            WebViewActivity.load(requireContext(), Store.SEARCH)
        }

        tips.setOnClickListener {
            WebViewActivity.load(requireContext(),Store.URL_MONEY_TIP)
        }

        val count = styleTabLayout.childCount

        for (i in 0 until count) {
            val styleTab = styleTabLayout.getChildAt(i) as StyleTab

            styleTab.setOnClickListener(onTabSelectListener)
        }


        EventBus.getDefault().register(this)

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMobileTabChange(e: MobileEvent) {
        select(0)
    }

    fun select(index: Int) {

        val count = styleTabLayout.childCount
        currentTab = index

        if (index == 3)
            if (TokenManager.isLoginValid())
                quickStartActivity(AdListActivity::class.java)
            else onAuthExpired()

        for (i in 0 until count) {
            val styleTab = styleTabLayout.getChildAt(i) as StyleTab
            styleTab.isChecked = i == index
        }

        fragmentChangeManager?.setFragments(index)

    }

    private val onTabSelectListener = View.OnClickListener { view ->
        val index = styleTabLayout.indexOfChild(view)
        select(index)
    }

    override fun onResume() {
        super.onResume()
        //如果是手游，就显示第一个tab
        if (currentTab == 3) {
            select(0)
            fragmentChangeManager?.setFragments(0)
        }
    }

    override fun onAttach(context: Context) {
//        Log.i(TAG, "onAttach: ")
        super.onAttach(context)
        statusBarHandler = context as FragmentChangeListener
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden);
//            statusBarHandler.setStatusBarColor(R.color.colorPrimary)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
//        Log.i(TAG, "onCreate: ")
//        retainInstance = true
        super.onCreate(savedInstanceState)
    }


    override fun onDestroyView() {
        super.onDestroyView()
//        Log.i(TAG, "onDestroyView: ")
    }

    override fun onDestroy() {
//        Log.i(TAG, "onDestroy: ")
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }
}
